// 组织架构管理 dept
import layer from '@/components/layer'
import permission from '@/components/permission'
import { formatDate } from '@/mixins/filters'
export default {
    data() {
        return {
            search: {
                loading: false,
                list: [],
                value: '',
                timer: null
            },
            deptDefaultProps: {
                label: 'name',
                children: 'childs'
            },
            // 当前数据
            currentData: {
                id: 0,
                name: ''
            },
            // 编辑layer数据
            editLayerData: {
                show: false,
                title: '添加',
                type: 0,          // 0:添加  1:修改  2:添加子级
                error: false,
                name: ''
            },
            // 删除layer数据
            deleteLayerData: {
                id: 0,
                userId: 0,
                show: false,
                title: '删除',
                tips: ''
            },
            // table数据
            tableData: {
                flag: false,
                total: 0,
                sizes: 10,
                size: 10,
                index: 0,
                data: []
            }
        }
    },
    computed: {
        deptList() {
            return this.$store.state.system.deptList
        }
    },
    components: {
        layer,
        permission
    },
    mounted() {
        if (this.$store.state.system.deptList && this.$store.state.system.deptList.length == 0) {
            this.$store.dispatch('getDeptList')
        }
    },
    methods: {
        // 搜索用户
        searchMethod(val) {
            if (this.search.timer) {
                clearTimeout(this.search.timer)
            }
            if (val !== '') {
                this.search.loading = true;
                this.search.timer = setTimeout(() => {
                    api.queryUser({
                        name: val,
                        pageSize: 100,
                        pageIndex: 1
                    }).then(res => {
                        this.search.loading = false
                        this.search.list = res.list
                    }).catch(res=>{
                        console.log(res)
                    })
                }, 200)
            } else {
                this.search.list = []
            }
        },
        // 搜索列表项改变
        searchChange(id) {
            this.$router.push('/system/user/detail?userid=' + id);
        },
        //  关闭编辑layer
        closeEditLayer() {
            this.editLayerData.name = ''
            this.editLayerData.error = false
        },
        // 显示编辑layer
        showEditLayer(type) {
            this.editLayerData.show = true
            this.editLayerData.type = type
            if (type == 0) {
                this.editLayerData.title = '添加类别'
                this.editLayerData.name = ''
            }
            else if (type == 1) {
                this.editLayerData.title = '修改类别'
                this.editLayerData.name = this.currentData.name
            }
            else if (type == 2) {
                this.editLayerData.title = '添加子类别'
                this.editLayerData.name = ''
            }
        },
        // 输入框失焦
        layerInputBlur() {
            if (!this.editLayerData.name) {
                this.editLayerData.error = true
            }
            else {
                this.editLayerData.error = false
            }
        },
        // 编辑提交
        editLayerConfirm() {
            if (!this.editLayerData.name) {
                this.editLayerData.error = true
            }
            else {
                let _type = this.editLayerData.type
                let _param = {
                    type: _type,
                    data: {
                        name: this.editLayerData.name
                    }
                }
                if (_type == 1) {
                    _param.data.id = this.currentData.id
                }
                else if (_type == 2) {
                    _param.data.parentId = this.currentData.id
                }
                api.editDept(_param).then(res => {
                    if (_type == 0) {
                        this.$msg.success('添加类别成功')
                    }
                    else if (_type == 1) {
                        this.$msg.success('修改类别成功')
                    }
                    else if (_type == 2) {
                        this.$msg.success('添加子类别成功')
                    }
                    this.$store.dispatch('getDeptList')
                    this.editLayerData.show = false
                    if (_type == 1) {
                        this.currentData.name = this.editLayerData.name
                    }
                    else {
                        this.currentData.id = 0
                        this.currentData.name = ''
                    }
                }).catch(res=>{
                    console.log(res)
                })
            }
        },
        // 显示删除layer 
        showDeleteLayer(userid) {
            this.deleteLayerData.show = true
            if (userid) {
                this.deleteLayerData.userId = userid
                this.deleteLayerData.tips = '确认删除该用户？'
            }
            else {
                this.deleteLayerData.userId = 0
                this.deleteLayerData.tips = '确认删除该类别名称？'
            }
        },
        // 删除提交
        deleteLayerConfirm() {
            // 删除用户
            if (this.deleteLayerData.userId) {
                api.deleteUser({
                    id: this.deleteLayerData.userId
                }).then(res => {
                    this.$msg.success('删除用户成功')
                    this.deleteLayerData.show = false
                    this.deleteLayerData.userId = 0
                    this.getTableData(this.tableData.index)
                }).catch(res=>{
                    console.log(res)
                })
            }
            // 删除类别
            else {
                api.deleteDept({
                    id: this.currentData.id
                }).then(res => {
                    this.$msg.success('删除类别成功')
                    this.deleteLayerData.show = false
                    this.currentData.id = 0
                    this.currentData.name = ''
                    this.$store.dispatch('getDeptList')
                }).catch(res=>{
                    console.log(res)
                })
            }
        },
        nodeClick(data) {
            this.$router.replace('/system/user?id=' + data.id)
            this.deleteLayerData.userId = 0
            this.currentData.id = data.id
            this.currentData.name = data.name
            this.tableData.flag = false
            this.getTableData(1)
        },
        // table每页条数改变
        tableSizeChange(val) {
            this.tableData.size = val
            this.tableData.flag = false
            this.getTableData(1)
        },
        // 获取table数据
        getTableData(index) {
            this.tableData.index = index
            api.queryUser({
                deptId: this.currentData.id,
                pageSize: this.tableData.size,
                pageIndex: index
            }).then(res => {
                if (!this.tableData.flag) {
                    this.tableData.total = res.totalCount
                    this.tableData.flag = true
                }
                this.tableData.data = []
                res.list.map(item => {
                    item.cdate = formatDate(item.cdate, 'yyyy-MM-dd')
                    return item
                })
                this.tableData.data = res.list
            }).catch(res=>{
                console.log(res)
            })
        }
    }
}
